# Maintainer: David Macek <david.macek.0@gmail.com>

# TODO: compile docs (uses pandoc, which is in Haskell)

_realname=clink
pkgbase=mingw-w64-${_realname}-git
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-git"
pkgver=0.4.4.r616.a60c0a0
pkgrel=1
pkgdesc="Bash's powerful command line editing in cmd.exe (mingw-w64)"
arch=('any')
url="https://mridgers.github.io/clink"
license=("GPL3")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-premake"
             "git")
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
source=("git+https://github.com/mridgers/clink.git"
        "0001-Relocation.patch")
sha256sums=('SKIP'
            '7d208979f4dd6bd40533ab5f3489b3bfb4023059d8cc0ffcc710c355fa18777d')

pkgver() {
  cd ${srcdir}/${_realname}
  printf "%s.r%s.%s" "$(git describe --tags --abbrev=0)" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cd "${srcdir}/${_realname}"
  git am "${srcdir}/0001-Relocation.patch"
  sed '/premake.override/d' -i premake4.lua
}

build() {
  local _dbg _bit
  case ${CARCH} in
  i686) _bit=32 ;;
  x86_64) _bit=64 ;;
  esac
  if check_option "debug" "y"; then
    _dbg=debug
  else
    _dbg=release
  fi

  cd "${srcdir}/${_realname}"
  premake4 --clink_ver="$(git describe --tags --abbrev=0)" gmake
  cd .build/gmake
  rm -rf obj/${_dbg}/x${_bit} bin/${_dbg}

  sed -r 's/AS_STR[(]([^)]+)[)]/"AS_STR(\1)"/g' -i *.make
  sed -r 's/[12]>nul//g' -i *.make
  sed -r 's/-m32.*/\0 -static-libgcc/' -i *.make
  sed 's~copy /y~cp~g' -i *.make
  sed 's~\\\*.lua"~"/*.lua~' -i *.make

  CC=gcc make config=${_dbg}${_bit}
}

package() {
  local _dbg _arch
  case ${CARCH} in
  i686) _arch=86 ;;
  x86_64) _arch=64 ;;
  esac
  if check_option "debug" "y"; then
    _dbg=debug
  else
    _dbg=release
  fi

  cd "${srcdir}/${_realname}/.build/gmake/bin/${_dbg}"

  mkdir -p "${pkgdir}${MINGW_PREFIX}/bin"
  cp clink_x${_arch}.exe clink_dll_x${_arch}.dll clink.bat "${pkgdir}${MINGW_PREFIX}/bin"

  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}"
  cp CHANGES "${srcdir}/${_realname}/docs/clink.md" "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}"

  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/${_realname}"
  cp *.lua clink_inputrc_base "${pkgdir}${MINGW_PREFIX}/share/${_realname}"

  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"
  cp LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"
}
